package cn.kgc.javaweb.service.impl;

import cn.kgc.javaweb.dao.UserDao;
import cn.kgc.javaweb.dao.impl.UserDaoImpl;
import cn.kgc.javaweb.entity.User;
import cn.kgc.javaweb.service.UserService;
import cn.kgc.javaweb.util.PageDate;

import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/10/31
 */
public class UserServiceImpl implements UserService {

    private UserDao  userDao = new UserDaoImpl();


    @Override
    public Boolean register(User user) {

       Integer line =  userDao.registerUser(user);

        return line !=null ;
    }

    @Override
    public Boolean checkUserName(String username) {

        User user =  userDao.findByUserName(username);   //  user==null 用户名不存在 可用

        return user==null;

    }

    @Override
    public User login(String username) {

        User user = userDao.findByUserName(username);

        return user ;
    }

    @Override
    public List<User> findAll() {

       List<User>  users =  userDao.selectAll();
        return users;
    }

    @Override
    public User findByUserName(String username) {
        User user = userDao.findByUserName(username);
        return user;
    }

    @Override
    public Boolean updateUserById(User user) {
        Integer line  =  userDao.updateUserById(user);
        return line!= null ;
    }

    @Override
    public Boolean deleteById(Long id) {

        Integer  line =   userDao.deleteById(id);
        return line!=null;
    }

    @Override
    public Boolean resetPwd(Long id) {
        Integer line  =   userDao.resetPwd(id);
        return  line!=null;
    }

    @Override
    public PageDate selectPage(Integer currentPage, Integer limit) {

        PageDate pageDate = new PageDate();
        pageDate.setCurrentPage(currentPage);
        pageDate.setLimit(limit);

        //  当前页显示的数据  分页查询  start  = (currentPage - 1)*limit    limit
        Integer start = (currentPage-1)*limit;
        List<User> usrs =  userDao.selectPage(start,limit);
        pageDate.setData(usrs);

        //数据的总条数
        List<User> users = userDao.selectAll();
        pageDate.setTotal(Long.valueOf(users.size()));


        return pageDate;
    }

    @Override
    public Boolean updateImg(String lastName, Long id) {

        Integer line =   userDao.updateImg(lastName,id);
        return   line!= null;
    }
}
